package com.work.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

import com.work.domain.UserAdmin;

/**
 * 
 * @author zhubingyi
 * 
 * @see   单点登陆
 *
 */
public class LoginListenter implements HttpSessionAttributeListener{
   private Map<String,HttpSession> map= new HashMap<String,HttpSession>();
		
	//向session中添加数据触发
	@Override
	public void attributeAdded(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		String name=event.getName();
		System.out.println(name);
		if(name.equals("adminuser")){
			UserAdmin user = (UserAdmin) event.getValue();
		    if(map.get(user.getAdminname())!=null){
		    	HttpSession session = map.get(user.getAdminname());
		    	session.removeAttribute(user.getAdminname());
		    	session.invalidate();
		    }
		    map.put(user.getAdminname(), event.getSession());
		}
	}
    //向session中移除数据触发
	@Override
	public void attributeRemoved(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		String name = event.getName();
		if(name.equals("adminuser")){
			UserAdmin user=(UserAdmin)event.getValue();
		    map.remove(user.getAdminname());
		}
	}

	@Override
	public void attributeReplaced(HttpSessionBindingEvent event) {
		// TODO Auto-generated method stub
		
	}
	public Map<String, HttpSession> getMap() {
		return map;
	}
	public void setMap(Map<String, HttpSession> map) {
		this.map = map;
	}

}
